import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { CrossBossIntrPanel } from "../view/crossBoss/CrossBossIntrPanel";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { ClockManager } from "../manager/ClockManager";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { MapIdRule } from "../rules/MapIdRule";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { CrossBossHintButton } from "../view/crossBoss/CrossBossHintButton";
import { SCrossBossWaitingRoomOpen } from "../../../Message/Public/SCrossBossWaitingRoomOpen";
import { SCrossBossJoins } from "../../../Message/Public/SCrossBossJoins";
import { ECopyType } from "../../../Message/Public/ECopyType";
import { CrossBossDetailModule } from "../../../modules/CrossBossDetailModule";
import { LayerManager } from "../manager/LayerManager";
import { SCrossBossGuildInfos } from "../../../Message/Public/SCrossBossGuildInfos";
import { SCrossBossGuildInfo } from "../../../Message/Public/SCrossBossGuildInfo";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { SCrossBossHatredInfo } from "../../../Message/Public/SCrossBossHatredInfo";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { SpritePlayer } from "../scene/player/entity/SpritePlayer";
import { EntityUtil } from "../scene/layer/utils/EntityUtil";
import { StaticResUrl } from "../resource/StaticResUrl";
type int = number;
//class CrossBossController
    
    export  class CrossBossController extends Controller
    {
        constructor()
        {
            
            super();this._hatredArray = [];
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.CopyEnterCrossBossEntrance, this.doEnterCrossBossEntrance);
            Dispatcher.addEventListener(EventName.CopyEnterCrossBossMap, this.doEnterCrossBossMap);
            Dispatcher.addEventListener(EventName.CopyEnterCrossBossCopy, this.onEnterCrossBossCopy);
            Dispatcher.addEventListener(EventName.CopyLeaveCrossBossCopy, this.onLeaveCrossBossCopy);
            Dispatcher.addEventListener(EventName.ClickBtnToCrossBossCopy, this.toEnterCrossBossHander);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossBossWaitingRoomOpenNotice, this.onCrossBossWaitingRoomOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossBossWaitingRoomInfo, this.onReceiveCrossBossWaitingRoomInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossBossStart, this.onCrossBossStart);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossBossHatredInfo, this.onReceiveHatredInfos);
            NetDispatcher.addCmdListener(ServerCommand.CrossBossRankingList, this.startShowList);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicUpgradeCrossBossStart, this.onUpgradeCrossBossStart);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicUpgradeCrossBossHatredInfo, this.onReceiveHatredInfos);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicUpgradeCrossBossWaitingRoomInfo, this.onReceiveCrossBossWaitingRoomInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicUpgradeCrossBossWaitingRoomOpenNotice, this.onUpgradeCrossBossWaitingRoomOpen);
            return;
        }

        public get crossBossInfoPanel(): CrossBossIntrPanel
        {
            if (!this._crossBossInfoPanel) 
            {
                this._crossBossInfoPanel = new CrossBossIntrPanel();
            }
            return this._crossBossInfoPanel;
        }

        /* internal  */doEnterCrossBossEntrance(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.copy.enterCrossBossEntrance(loc1);
            return;
        }

        /* internal  */doEnterCrossBossMap(arg1: DataEvent): void
        {
            GameProxy.copy.enterCrossBoss();
            return;
        }

        /* internal  */onEnterCrossBossCopy(arg1): void
        {
            Dispatcher.addEventListener(EventName.Scene_AddEntity, this.onAddEntityHandler);
            this.updateEntitysTitle();
            return;
        }

        /* internal  */onLeaveCrossBossCopy(arg1): void
        {
            Dispatcher.removeEventListener(EventName.Scene_AddEntity, this.onAddEntityHandler);
            this._hatredArray = [];
            this.updateEntitysTitle();
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1): void
        {
            if (GameMapUtil.curMapState.isEnterCorssBossMap70 || GameMapUtil.curMapState.isEnterCorssBossMap90) 
            {
                this.crossBossInfoPanel.show();
                this.crossBossInfoPanel.updatePosition();
                this.crossBossInfoPanel.updateJoinInfo(cache.crossBoss.myGuildJoinInfo);
                if (cache.crossBoss.waitingRoomOpenInfo) 
                {
                    this.crossBossInfoPanel.updateCountDownTimer(cache.crossBoss.waitingRoomOpenInfo.closeDt);
                }
                else 
                {
                    this.crossBossInfoPanel.updateCountDownTimer(ClockManager.instance.nowDate);
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            }
            else if (this._crossBossInfoPanel && !this._crossBossInfoPanel.isHide) 
            {
                this._crossBossInfoPanel.hide();
            }
            if (this._crossBossDetailModule70 && !this._crossBossDetailModule70.isHide) 
            {
                this._crossBossDetailModule70.upDateMouseEvent(!(MapFileUtil.mapID == MapIdRule.CrossBossEntryMap70));
            }
            if (this._crossBossDetailModule90 && !this._crossBossDetailModule90.isHide) 
            {
                this._crossBossDetailModule90.upDateMouseEvent(!(MapFileUtil.mapID == MapIdRule.CrossBossEntryMap90));
            }
            return;
        }

        /* internal  */onCrossBossWaitingRoomOpen(arg1: MessageBlock): void
        {
            var loc1=null;
            if (arg1 && cache.role.roleInfo.level < 70) 
            {
                return;
            }
            if (!this._hintButton70) 
            {
                this._hintButton70 = new CrossBossHintButton(70);
                this._hintButton70.addEventListener(flash.events.MouseEvent.CLICK, this.onHintButtonClick);
            }
            this._hintButton70.show();
            if (arg1 && arg1.messageBase) 
            {
                loc1 = arg1.messageBase as SCrossBossWaitingRoomOpen;
                cache.crossBoss.waitingRoomOpenInfo = loc1;
                this._hintButton70.updateTime(loc1);
                if (this._crossBossInfoPanel && !this._crossBossInfoPanel.isHide) 
                {
                    this._crossBossInfoPanel.updateCountDownTimer(loc1.closeDt);
                }
            }
            return;
        }

        /* internal  */onReceiveCrossBossWaitingRoomInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCrossBossJoins;
            cache.crossBoss.waitionRoomInfo = loc1;
            if (this._crossBossInfoPanel && !this._crossBossInfoPanel.isHide) 
            {
                this._crossBossInfoPanel.updateJoinInfo(cache.crossBoss.myGuildJoinInfo);
            }
            return;
        }

        /* internal  */onHintButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=0;
            if (arg1.currentTarget != this._hintButton70) 
            {
                if (arg1.currentTarget == this._hintButton90) 
                {
                    loc1 = ECopyType._ECopyUpgradeCrossBoss;
                    if (!this._crossBossDetailModule90) 
                    {
                        this._crossBossDetailModule90 = new CrossBossDetailModule(90, LayerManager.windowLayer);
                    }
                    this._crossBossDetailModule90.show();
                    this._crossBossDetailModule90.upDateMouseEvent(!(MapFileUtil.mapID == MapIdRule.CrossBossEntryMap90));
                }
            }
            else 
            {
                loc1 = ECopyType._ECopyCrossBoss;
                if (!this._crossBossDetailModule70) 
                {
                    this._crossBossDetailModule70 = new CrossBossDetailModule(70, LayerManager.windowLayer);
                }
                this._crossBossDetailModule70.show();
                this._crossBossDetailModule70.upDateMouseEvent(!(MapFileUtil.mapID == MapIdRule.CrossBossEntryMap70));
            }
            this.startGetCrossRankingList(loc1);
            return;
        }

        /* internal  */startGetCrossRankingList(arg1: int): void
        {
            GameProxy.copy.getCrossBossGuildInfos_async(arg1);
            return;
        }

        /* internal  */startShowList(arg1: SCrossBossGuildInfos): void
        {
            var loc1=0;
            if (arg1.crossBossGuildInfos && arg1.crossBossGuildInfos.length > 0) 
            {
                loc1 = (arg1.crossBossGuildInfos[0] as SCrossBossGuildInfo).copyType;
            }
            if (this._crossBossDetailModule70 && !this._crossBossDetailModule70.isHide) 
            {
                if (loc1 == 0 || loc1 == ECopyType._ECopyCrossBoss) 
                {
                    this._crossBossDetailModule70.updateView(arg1.crossBossGuildInfos, arg1.myGuildScore);
                }
            }
            if (this._crossBossDetailModule90 && !this._crossBossDetailModule90.isHide) 
            {
                if (loc1 == 0 || loc1 == ECopyType._ECopyUpgradeCrossBoss) 
                {
                    this._crossBossDetailModule90.updateView(arg1.crossBossGuildInfos, arg1.myGuildScore);
                }
            }
            return;
        }

        /* internal  */toEnterCrossBossHander(arg1: DataEvent): void
        {
            var loc1=arg1.data["target"] as TaskTargetInfo;
            if (arg1.data["type"] != "goto") 
            {
                if (arg1.data["type"] == "sendto") 
                {
                    AIManager.onAIControl(loc1, AIType.AI_Convey);
                }
            }
            else 
            {
                AIManager.onAutoPathAIControl(loc1);
            }
            return;
        }

        /* internal  */onCrossBossStart(arg1: MessageBlock): void
        {
            if (this._hintButton70 && !this._hintButton70.isHide) 
            {
                this._hintButton70.hide();
            }
            if (this._crossBossInfoPanel && !this._crossBossInfoPanel.isHide) 
            {
                this._crossBossInfoPanel.updateCountDownTimer(ClockManager.instance.nowDate);
            }
            return;
        }

        /* internal  */onReceiveHatredInfos(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCrossBossHatredInfo;
            this._hatredArray = loc1.entityIds;
            this.updateEntitysTitle();
            return;
        }

        /* internal  */updateEntitysTitle(): void
        {
            var loc2=false;
            var loc3=null;
            var loc4=null;
            var loc1=ThingUtil.entityUtil.getAllUserList();
            var loc5=0;
            while (loc5 < loc1.length) 
            {
                if (loc4 = loc1[loc5] as SpritePlayer) 
                {
                    loc2 = false;
                    var loc6=0;
                    var loc7=this._hatredArray;
                    for(loc3 of loc7) 
                    {
                        if (EntityUtil.toString(loc3) != loc4.entityID) 
                        {
                            continue;
                        }
                        loc2 = true;
                        break;
                    }
                    if (cache.copy.copyInfo && cache.copy.copyInfo.copyType == ECopyType._ECopyUpgradeCrossBoss) 
                    {
                        loc4.updateTitleEx(StaticResUrl.CrossBossMaxHurtTitle, loc2);
                    }
                    else 
                    {
                        loc4.updateTitleEx(StaticResUrl.CrossBossMaxHurtTitle, loc2);
                    }
                }
                ++loc5;
            }
            return;
        }

        /* internal  */onAddEntityHandler(arg1: DataEvent): void
        {
            this.updateEntitysTitle();
            return;
        }

        /* internal  */onUpgradeCrossBossStart(arg1: MessageBlock): void
        {
            if (this._hintButton90 && !this._hintButton90.isHide) 
            {
                this._hintButton90.hide();
            }
            if (this._crossBossInfoPanel && !this._crossBossInfoPanel.isHide) 
            {
                this._crossBossInfoPanel.updateCountDownTimer(ClockManager.instance.nowDate);
            }
            return;
        }

        /* internal  */onUpgradeCrossBossWaitingRoomOpen(arg1: MessageBlock): void
        {
            var loc1=null;
            if (arg1 && cache.role.roleInfo.level < 90) 
            {
                return;
            }
            if (cache.role.roleInfo.level >= 90) 
            {
                if (!this._hintButton90) 
                {
                    this._hintButton90 = new CrossBossHintButton(90);
                    this._hintButton90.addEventListener(flash.events.MouseEvent.CLICK, this.onHintButtonClick);
                }
                this._hintButton90.show();
            }
            if (arg1 && arg1.messageBase) 
            {
                loc1 = arg1.messageBase as SCrossBossWaitingRoomOpen;
                cache.crossBoss.waitingRoomOpenInfo = loc1;
                if (this._hintButton90) 
                {
                    this._hintButton90.updateTime(loc1);
                }
                if (this._crossBossInfoPanel && !this._crossBossInfoPanel.isHide) 
                {
                    this._crossBossInfoPanel.updateCountDownTimer(loc1.closeDt);
                }
            }
            return;
        }

        private /* var */_hintButton70: CrossBossHintButton;

        private /* var */_crossBossDetailModule70: CrossBossDetailModule;

        private /* var */_hintButton90: CrossBossHintButton;

        private /* var */_crossBossDetailModule90: CrossBossDetailModule;

        private /* var */_crossBossInfoPanel: CrossBossIntrPanel;

        private /* var */_hatredArray: Array<any>;
    }
